package cn.wang.web.cargo.shppingorder;

import cn.wang.entity.cargo.packing.PackingList;
import cn.wang.entity.cargo.shippingorder.ShippingOrder;
import cn.wang.service.cargo.packing.PackingListService;
import cn.wang.service.cargo.shippingorder.ShippingOrderService;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("shippingOrderAction")
@Scope("prototype")
public class ShippingOrderAction extends BaseAction implements ModelDriven<ShippingOrder> {

    @Autowired
    private ShippingOrderService shippingOrderService;
    @Autowired
    private PackingListService packingListService;
    private Pagination page = new Pagination();

    private List<ShippingOrder> shippingOrderList;

    private ShippingOrder model = new ShippingOrder();

    public String list() {

         page = shippingOrderService.findPage(page);
        page.setUrl("shippingOrderAction_list");
        push(page);
        return "list";
    }

    public String tocreate() {
        //查询出所有装箱列表
        page = packingListService.findPage(page.getPageNo(), page.getPageSize(), 1l);
        page.setUrl("shippingOrderAction_tocreate");
        push(page);;
        return "tocreate";
    }

    public String insert() {
        shippingOrderService.save(model);
        return list();
    }

    public String toupdate() {
        ShippingOrder shippingOrder = shippingOrderService.findById(model.getId());
        super.push(shippingOrder);

        shippingOrderList = shippingOrderService.findAll();
        shippingOrderList.remove(shippingOrder);
        return "toupdate";
    }

    public String update() {
        shippingOrderService.update(model);
        return list();
    }

    /**
     * @return
     * 一对一级联删除时逻辑放在 action能成功
     * 放在service层 会报错
     */
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        for (String id:ids){
            shippingOrderService.deleteById(id);
            PackingList packingList=packingListService.findById(id);
            packingList.setState(1l);
            packingListService.update(packingList);
        }
        return "rlist";

    }

    public String toview() {
        ShippingOrder shippingOrder = shippingOrderService.findById(model.getId());
        push(shippingOrder);
        return "toview";
    }

    public String submit(){
        //修改状态
        shippingOrderService.submit(model);
        return "rlist";
    }

    public String cancel(){
        shippingOrderService.cancel(model);
        return "rlist";
    }
    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<ShippingOrder> getShippingOrderList() {
        return shippingOrderList;
    }

    @Override
    public ShippingOrder getModel() {
        return model;
    }
}
